package com.huaxin.hxcloud.work;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huamaitel.api.HMDefines;
import com.huaxin.common.BaseApp;
import com.huaxin.hxcloud.R;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author songxf
 *         created at 2017/7/10 15:11
 * @desc 华迈视频 Adapter
 */

public class HxHuaMaiAdapter extends RecyclerView.Adapter<HxHuaMaiAdapter.HxHuaMaiViewHolder> {
    private static final String TAG = "HxHuaMaiAdapter";
    private       List<Map<String, Object>> mNodeDatas;
    private       Context                   mContext;
    private final LayoutInflater            mInflater;
    private int mVideoStream = HMDefines.CodeStream.MAIN_STREAM;

    public HxHuaMaiAdapter(Context context, List<Map<String, Object>> nodeDatas) {
        this.mContext = context;
        this.mNodeDatas = nodeDatas;
        mInflater = LayoutInflater.from(mContext);

    }


    @Override
    public HxHuaMaiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_huamai_video, parent, false);
        return new HxHuaMaiViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final HxHuaMaiViewHolder holder, int position) {
        Map<String, Object> objectMap = mNodeDatas.get(position);
        final int nodeType = (Integer) objectMap.get("type");
        final int nodeId = (Integer) objectMap.get("id");
        String name = (String) objectMap.get("name");
        Log.d(TAG, "nodeType:" + nodeType + "，nodeId:" + nodeId + "，nodeName:" + name);

        BaseApp.curNodeHandle = nodeId;
        holder.mVideoTv.setText(name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /**
                 * TODO：异常处理 此处注意判断设备是否在线 请参考SDK文档 添加 isOnline这个接口进行判断
                 * 不在线的设备不能点击，不能进入下一步操作。
                 */
                if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_DVS || nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_GROUP) {
                    BaseApp.rootList.add(nodeId);
                    //getChildrenByNodeId(nodeId);

                }
                if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_CHANNEL) {
                    /**
                     * 注意：针对DVS设备，在获取NodeId时，需要获取父类的Id（getParentId）,用于登录设备。
                     * 通道信息用于获取DVS下面对应的设备通道号
                     */
                    int nodeDvsId = BaseApp.getJni().getParentId(nodeId);
                    HMDefines.ChannelInfo info = BaseApp.getJni().getChannelInfo(nodeId); // 获取通道信息
                    //如果设备在线
                    Log.d(TAG, "info:" + info.index + "+" + info.name);
                    gotoPlayActivity(nodeDvsId, info.index, mVideoStream);
                } else if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_DEVICE) {
                    gotoPlayActivity(nodeId, 0, mVideoStream);

                }
            }
        });


    }

    private void gotoPlayActivity(int nodeId, int channel, int videoStream) {
        Intent intent = new Intent();
        intent.setClass(mContext, HxHuaMaiPlayActivity.class);
        intent.putExtra(BaseApp.NODE_ID, nodeId);
        intent.putExtra(BaseApp.CHANNEL, channel);
        intent.putExtra(BaseApp.VIDEO_STREAM, videoStream);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        BaseApp.mIsUserLogin = true;
        mContext.startActivity(intent);
    }


    @Override
    public int getItemCount() {
        Log.d(TAG, "getItemCount: " + mNodeDatas.size());
        return mNodeDatas == null ? 0 : mNodeDatas.size();
    }


    class HxHuaMaiViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.video_tv)
        TextView mVideoTv;

        HxHuaMaiViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
